package com.woniuxy.mq02.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.mq02.entity.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author author
 * @Create 2024-02-29 19:15
 */
@Component
public class RabbitConsumer {
    //指定当前方法，监听队列，接收消息
    //这个消费方法可以带三个参数
    // String/Object str, 消息体
    // Message message, 完整的消息对象
    // Channel channel，通信管道
    @RabbitListener(queues = "helloworld")
    public void receiveHelloworld(String str, Message message, Channel channel){
        System.out.println("监听helloworld队列，处理消息");
        System.out.println("str:"+str);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        System.out.println("-----------------");
    }
    @RabbitListener(queues = "queueemail")
    public void receiveEmail(User user, Message message, Channel channel) throws IOException {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("监听email队列，发送邮件");
        System.out.println("user:"+user);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        //消费者手动确认，业务代码执行成功以后，必须调用方法，告诉服务器，确认处理成功，服务器才会删除消息
        //long deliveryTag, 投递的标记
        // boolean multiple，多操作，true:将小于当前标记的消息都确认，
        // 我们一般使用false,我们正常的处理事每次获取一个消息，处理成功以后删除这个消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("-----------------");
    }
    @RabbitListener(queues = "queuesms")
    public void receiveSms(String str, Message message, Channel channel){
        System.out.println("监听sms队列，发送短信");
        System.out.println("str:"+str);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        System.out.println("-----------------");
    }
}
